package com.diver.flinkdemo.mapper.mysqlMapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.diver.flinkdemo.dto.xxt.TodaysSportDto;
import com.diver.flinkdemo.entity.xxt.DayExercise;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DayExerciseMapper extends BaseMapper<DayExercise> {

    @Update("update day_exercise set step = #{step} , distance = #{distance} , heat = #{heat}  where student_card_id=#{studentCardId} and create_time = #{createTime}")
    void doUpdateModelByStuId(DayExercise dayExercise);

    @Delete("delete from day_exercise where student_card_id=#{studentCardId}")
    int delByStudentCardId(String studentCardId);

    /**
     * 通过日期 获取某天的步数
     * @author yangs
     * @param studentCardId
     * @param currentTime 日期时间戳（某天零点）
     * @return
     */
    @Select("select id, student_card_id, step, rank, distance, heat, create_time,school_id,school_name,grade_id,grade_name,class_name from day_exercise " +
            "where student_card_id=#{studentCardId} and create_time = #{currentTime} limit 1")
    DayExercise getStepByDate(@Param("studentCardId") String studentCardId,
                         @Param("currentTime") Long currentTime);

    /**
     * 查询某天班级步数列表
     * @param de
     * @return
     */
    @Select("select student_card_id,step,distance,heat,create_time from day_exercise where grade_id =  #{gradeId} and school_id = #{schoolId}  " +
            " and class_name = #{className} and  create_time = #{createTime}")
    List<TodaysSportDto> getClassStepByDate(DayExercise de);

    /**
     * 查询当月每天步数列表
     * @param studentCardId
     * @return
     */
    @Select("select student_card_id,step,distance,heat,create_time from day_exercise where student_card_id = #{studentCardId} and " +
            "DATE_FORMAT(FROM_UNIXTIME(create_time/1000), '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )  " +
            "order by create_time desc ")
    List<TodaysSportDto> getTodaysSportAndMonth(@Param("studentCardId")String studentCardId);

    /**
     * 查询 #{newUploadDate} 日期有上报数据的设备该月的运动总和
     * @param newUploadDate
     * @return
     */
    @Select("select student_card_id,CAST(sum(step) as SIGNED) step, sum(distance) distance,sum(heat) heat,school_id,school_name,grade_id,grade_name,class_name,#{newUploadDate} create_time " +
            "from day_exercise " +
            "where DATE_FORMAT(from_unixtime(create_time/1000),'%Y-%m') = DATE_FORMAT(from_unixtime(#{newUploadDate}/1000),'%Y-%m') " +
            "and student_card_id in(select student_card_id from day_exercise where create_time = #{newUploadDate}) " +
            "group by student_card_id")
    public List<DayExercise> sumDayExercise(@Param("newUploadDate")Long newUploadDate);
}
